﻿using System;
using System.Windows.Data;

namespace Kaesoft.Hospedaje.Common
{
    /// <summary>
    /// Convertir de String.Empty a null en un binding
    /// </summary>
    public class NullConverter : IValueConverter
    {
        #region IValueConverter Members
         #region Public methods
        /// <summary>
        /// Sin implementación
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns>El valor de entrada</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }

        /// <summary>
        /// Convertir de String.Empty a null
        /// </summary>
        /// <param name="value">Stringa a ser convertido</param>
        /// <param name="targetType">String</param>
        /// <param name="parameter">Parámetro para convertir</param>
        /// <param name="culture">Cultura actual</param>
        /// <returns>Valor convertido</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //If string is empty, then return null
            if (string.IsNullOrEmpty(value.ToString()))
                return null;
            return value;

        }
        #endregion
        #endregion
    }

    /// <summary>
    /// Convertir de cero a null
    /// </summary>
    public class ZeroToNullConverter : IValueConverter
    {
        #region IValueConverter Members
        #region Public methods
        /// <summary>
        /// Si el valor es cero, retorna null
        /// </summary>
        /// <param name="value">Valor a convertir</param>
        /// <param name="targetType">Numeric type</param>
        /// <param name="parameter">Parámetro del convertidor</param>
        /// <param name="culture">Cultura actual</param>
        /// <returns>Valor convertido</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ((value is decimal && ((decimal)value) == 0)
                || ((value is int && ((int)value) == 0))
                || (value is float && ((float)value) == 0))
                return null;
            return value;
        }

        /// <summary>
        /// Sin implementación
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns>El valor de entrada</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
        #endregion
        #endregion
    }
}